package com.vogue.supplymall.order.common;

import com.vogue.supplymall.common.domain.dto.OrderDetailInfo;
import com.vogue.supplymall.common.domain.dto.OrderInfo;
import com.vogue.supplymall.common.domain.dto.RefundMaxCountInfo;
import com.vogue.supplymall.common.domain.dto.RefundOrderDetailInfo;

import java.util.List;
import java.util.Map;

/**
 * The type Check order utils.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-01-23
 */
public class CheckOrderUtils {

    /**
     * Check add order parameter boolean.
     * 添加订单时的参数check
     *
     * @param info          the info
     * @param loginUserType the login user type
     * @return the boolean
     */
    public static Boolean checkAddOrderParameter(OrderInfo info, Integer loginUserType) {
        return null == info.getShopId()
                || null == info.getOid()
                || null == info.getAddress()
                || null == info.getGoodsData();
    }

    /**
     * Check confirm order parameter boolean.
     * 确认订单时的参数check
     *
     * @param info          the info
     * @param loginUserType the login user type
     * @return the boolean
     */
    public static Boolean checkConfirmOrderParameter(OrderInfo info, Integer loginUserType) {
        return null == info.getShopId()
                || null == info.getOid()
                || null == info.getOipids()
                || null == info.getOiids();

    }

    /**
     * Check order different boolean.
     * 提交订单时，和最新的商品信息比较差异
     * 有差异返回true，没差异返回false
     *
     * @param current the current
     * @param older   the older
     * @return the boolean
     */
    public static Boolean checkOrderDifferent(List<Map<String, Object>> current, List<Map<String, Object>> older) {
        if (null == current || null == older) {
            return true;
        }

        if (current.size() != older.size()) {
            return true;
        }
        for (Map<String, Object> product : current) {
            if (compare(product, older)) {
                return true;
            }
        }
        return false;
    }

    private static Boolean compare(Map<String, Object> current, List<Map<String, Object>> older) {
        /*查看订单商品是否和最新商品一致*/
        for (Map<String, Object> old : older) {
            if (current.get("oid").equals(old.get("oid"))
                    && current.get("oiid").equals(old.get("oiid"))
                    && current.get("oipid").equals(old.get("oipid"))) {
                for (Map.Entry<String, Object> entry : old.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    if (!current.get(key).equals(value.toString())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Check order detail parameter boolean.
     * 订单详情的参数check
     *
     * @param orderDetailInfo the order detail info
     * @return the boolean
     */
    public static Boolean checkOrderDetailParameter(OrderDetailInfo orderDetailInfo) {
        return null == orderDetailInfo.getOrderid();
    }

    /**
     * Check order detail parameter boolean.
     * 退单详情的参数check
     *
     * @param refundOrderDetailInfo the refund order detail info
     * @return the boolean
     */
    public static Boolean checkRefundOrderDetailParameter(RefundOrderDetailInfo refundOrderDetailInfo) {
        return null == refundOrderDetailInfo.getRefundorderid();
    }

    /**
     * Check order detail parameter boolean.
     * 可退数量的参数check
     *
     * @param refundMaxCountInfo the refund max count info
     * @return the boolean
     */
    public static Boolean checkRefundMaxQuantityParameter(RefundMaxCountInfo refundMaxCountInfo) {
        return (null == refundMaxCountInfo.getOipid() || null == refundMaxCountInfo.getOrderid());
    }

    /**
     * Check order detail parameter boolean.
     * 订单详情的参数check
     *
     * @param orderDetailInfo the order detail info
     * @return the boolean
     */
    public static Boolean checkUpdateFreightParameter(OrderDetailInfo orderDetailInfo) {
        return null == orderDetailInfo.getOrderid()
                || null == orderDetailInfo.getStatusid()
                || null == orderDetailInfo.getOrderstatus()
                || null == orderDetailInfo.getFreight()
                || null == orderDetailInfo.getBuyerSellerType();
    }

    /**
     * Check order detail parameter boolean.
     * 订单详情的参数check
     *
     * @param orderDetailInfo the order detail info
     * @return the boolean
     */
    public static Boolean checkUpdateStatusParameter(OrderDetailInfo orderDetailInfo) {
        return null == orderDetailInfo.getOrderid()
                || null == orderDetailInfo.getStatusid()
                || null == orderDetailInfo.getOrderstatus()
                || null == orderDetailInfo.getAction();
    }
}
